<template>
    <div class="app-container">
       <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
          <el-form-item label="图标名称" prop="name">
             <el-input
                v-model="queryParams.name"
                placeholder="请输入图标名称"
                clearable
                style="width: 200px"
                @keyup.enter="handleQuery"
             />
          </el-form-item>
          <el-form-item>
             <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
             <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
       </el-form>
 
       <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
             <el-button
                type="primary"
                plain
                icon="Plus"
                @click="handleAdd"
                v-hasPermi="['system:setting:iconAdd']"
             >新增</el-button>
          </el-col>
       </el-row>
 
       <el-table v-loading="loading" :data="postList">
            <el-table-column
                label="序号"
                width="60"
                type="index"
            ></el-table-column>
          <el-table-column label="图标名称" align="center" prop="name" />
            <el-table-column label="图标" align="center" prop="loadUrl" width="90" >
            <template #default="scope">
              <img v-if="scope.row.loadUrl" :src="scope.row.loadUrl" style="width:32px;height:32px;object-fit:contain;" />
            </template>
            </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" />
          <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
             <template #default="scope">
                <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:setting:iconDelete']">删除</el-button>
             </template>
          </el-table-column>
       </el-table>
 
       <!-- <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
       /> -->
 
       <!-- 添加或修改岗位对话框 -->
       <el-dialog title="添加图标" v-model="open" width="500px" append-to-body>
          <el-form ref="postRef" :model="form" :rules="rules" label-width="80px">
             <el-form-item label="图标名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入图标名称" />
             </el-form-item>
             <!-- 上传图标 -->
             <el-form-item label="图标" prop="fileList">
               <el-upload
                  class="upload-demo"
                  :limit="1"
                  :file-list="form.fileList"
                  :auto-upload="false"
                  :on-preview="handlePreview"
                  :on-remove="handleRemove"
                  :on-change="handleFileChange"
                  list-type="picture-card"
                  accept="image/*"
                  >
                  <el-icon><Plus /></el-icon>
                  </el-upload>
             </el-form-item>
          </el-form>
          <template #footer>
             <div class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
             </div>
          </template>
       </el-dialog>
       <!-- 图片预览弹窗 -->
      <el-dialog v-model="previewVisible" title="图片预览">
         <img :src="previewImage" style="width: 100%" />
      </el-dialog>
    </div>
 </template>
 
 <script setup name="Post">
 import { iconList, uploadPic, iconDelete } from "@/api/system/icon";
 
 const { proxy } = getCurrentInstance();
 const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
 
 const postList = ref([]);
 const open = ref(false);
 const loading = ref(false);
 const showSearch = ref(true);
 const ids = ref([]);
 const total = ref(0);
 
 const data = reactive({
   form: {},
   queryParams: {
     pageNum: 1,
     pageSize: 10,
     name: undefined,
   },
   rules: {
     postName: [{ required: true, message: "岗位名称不能为空", trigger: "blur" }],
   }
 });
 
 const { queryParams, form, rules } = toRefs(data);
 
 /** 查询岗位列表 */
 function getList() {
   loading.value = true;
   iconList(queryParams.value).then(response => {
     postList.value = response.data;
     total.value = response.total;
     loading.value = false;
   });
 }

 getList() 
 
 /** 取消按钮 */
 function cancel() {
   open.value = false;
   reset();
 }
 
 /** 表单重置 */
 function reset() {
   form.value = {
     name: '',
     fileList: [],
   };
   proxy.resetForm("postRef");
 }
 
 /** 搜索按钮操作 */
 function handleQuery() {
   queryParams.value.pageNum = 1;
   getList();
 }
 
 /** 重置按钮操作 */
 function resetQuery() {
   proxy.resetForm("queryRef");
   handleQuery();
 }

 /** 新增按钮操作 */
 function handleAdd() {
   reset();
   open.value = true;
 }
 
 /** 提交按钮 */
 function submitForm() {
   proxy.$refs["postRef"].validate(valid => {
     if (valid) {
      const formData = new FormData()
      formData.append('name', form.value.name)
      formData.append('file', form.value.fileList[0].raw)
      uploadPic(formData).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
     }
   });
 }

const previewImage = ref('')

function handlePreview(file) {
  previewImage.value = file.url || URL.createObjectURL(file.raw)
  previewVisible.value = true
}

const handleRemove = (file, fileList) => {
  form.value.fileList = fileList
}

function handleFileChange(file, currentFileList) {
  form.value.fileList = currentFileList
}
 
 /** 删除按钮操作 */
 function handleDelete(row) {
   const postIds = row.id;
   proxy.$modal.confirm('是否确认删除该数据项？').then(function() {
     return iconDelete(postIds);
   }).then(() => {
     getList();
     proxy.$modal.msgSuccess("删除成功");
   }).catch(() => {});
 }

 </script>
 